const mysql = require("mysql")


module.exports = {
    exec(sql, params, callback) {
        let conn = mysql.createConnection({
            host: "127.0.0.1",
            user: "root",
            password: "",
            database: "aolaigou"

        })

        conn.connect(function(err) {

            if (!err) {
                console.log("连接成功");
            } else {
                console.log("连接失败:" + err.message);
            }

        })

        conn.query(sql, params, callback)

        conn.resume()
    },
    execAsync(sql, params) {
        return new Promise(function(resolve, reject) {
            let conn = mysql.createConnection({
                host: "127.0.0.1",
                user: "root",
                password: "",
                database: "aolaigou",
                port: 3306
            })

            conn.connect(function(err) {
                if (!err) {
                    console.log('连接成功')
                } else {
                    console.log("链接失败:" + err.message);
                    reject(err.message)
                }
            })
            conn.query(sql, params, function(err, result) {
                if (!err) {
                    resolve(result);
                } else {
                    reject(err)
                }
            });
            conn.resume();

        })
    }
}